import smtplib
from email.mime.text import MIMEText
from email.header import Header


class Email:
    def __init__(self):
        self.sender = "1161127823@qq.com"  # “我”的邮箱
        self.password = "tobkujbhmxekhgac"  # 密码
        self.subject = "尊敬的用户您好"  # 邮件标题
        self.smtpserver = 'smtp.qq.com'

    def run(self, name, code, email):
        # words = "您正在注册xx论坛， 请点击以下的链接激活账号， 如不是您本人注册的请忽略\n"
        words = """<html><head><base target="_blank">
                        <style type="text/css">
                        ::-webkit-scrollbar{ display: none; }
                        </style>
                        <style id="cloudAttachStyle" type="text/css">
                        #divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}
                        </style>
                        </head>
                        <body tabindex="0" role="listitem">                                                           
                        <div id="content">     
                        <table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank">xx社区</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">%s</em>，请完成以下操作： </p> <div style=""> <a href="http://www.tnan.top/user/activate?token=%s" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">立即激活邮箱</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table>            
                        </div>
                        <script>var _c=document.getElementById('content');_c.innerHTML=(_c.innerHTML||'').replace(/(href|formAction|onclick|javascript)/ig, '__$1');</script>
                        <style type="text/css">
                        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
                        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
                        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%%}
                        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
                        img{ border:0}
                        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
                        blockquote{margin-right:0px}
                        </style>
                        <style id="netease_mail_footer_style" type="text/css">#netease_mail_footer{display:none;}</style>
                        <style id="ntes_link_color" type="text/css">a,td a{color:#064977}</style>
                        </body></html>""" % (name, code)
        msg = MIMEText(words, 'html', 'utf-8')  # 中文需参数‘utf-8'，单字节字符不需要
        msg['Subject'] = Header(self.subject, 'utf-8')  # 邮件标题
        msg['from'] = self.sender  # 发信人地址
        msg['to'] = email  # 收信人地址
        # server = smtplib.SMTP_SSL(server, 465)
        server = self.smtpserver
        server = smtplib.SMTP_SSL(server, 465)
        # server.set_debuglevel(1)
        server.login(self.sender, self.password)
        server.sendmail(self.sender, email, msg.as_string())

        # smtp = smtplib.SMTP()
        # smtp.connect('smtp.qq.com')
        # smtp.login(self.sender, self.password)
        # smtp.sendmail(self.sender, email, msg.as_string())  # 这行代码解决的下方554的错误
        server.quit()


class Reass:
    def __init__(self):
        self.sender = "1161127823@qq.com"  # “我”的邮箱
        self.password = "tobkujbhmxekhgac"  # 密码
        self.subject = "尊敬的用户您好"  # 邮件标题
        self.smtpserver = 'smtp.qq.com'

    def run(self, name, code, email):
        # words = "您正在注册xx论坛， 请点击以下的链接激活账号， 如不是您本人注册的请忽略\n"
        words = """<html><head><base target="_blank">
                        <style type="text/css">
                        ::-webkit-scrollbar{ display: none; }
                        </style>
                        <style id="cloudAttachStyle" type="text/css">
                        #divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}
                        </style>
                        </head>
                        <body tabindex="0" role="listitem">                                                           
                        <div id="content">     
                        <table border="0" cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid #ddd; border-radius: 3px; color: #555; font-size: 12px; height: auto; margin: auto; overflow: hidden; text-align: left; word-break: break-all; word-wrap: break-word;"> <tbody style="margin: 0; padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin: 0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color: #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size: 14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;" href="http://fly.layui.com/" target="_blank">xx社区</a></div> </td> </tr> <tr style="margin: 0; padding: 0;"> <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px; margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight: 700;">%s</em>，请完成以下操作： </p> <div style=""> <a href="http://www.tnan.top/reset_pass?token=%s&email=%s" style="background-color: #009E94; color: #fff; display: inline-block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0 15px; text-decoration: none;" target="_blank">立即重设密码</a> </div> <p style="line-height: 20px; margin-top: 20px; padding: 10px; background-color: #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿点击！</p> </td> </tr> <tr style="background-color: #fafafa; color: #999; height: 35px; margin: 0; padding: 0; text-align: center;"> <td style="margin: 0; padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table>            
                        </div>
                        <script>var _c=document.getElementById('content');_c.innerHTML=(_c.innerHTML||'').replace(/(href|formAction|onclick|javascript)/ig, '__$1');</script>
                        <style type="text/css">
                        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
                        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}
                        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%%}
                        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
                        img{ border:0}
                        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
                        blockquote{margin-right:0px}
                        </style>
                        <style id="netease_mail_footer_style" type="text/css">#netease_mail_footer{display:none;}</style>
                        <style id="ntes_link_color" type="text/css">a,td a{color:#064977}</style>
                        </body></html>""" % (name, code, email)
        msg = MIMEText(words, 'html', 'utf-8')  # 中文需参数‘utf-8'，单字节字符不需要
        msg['Subject'] = Header(self.subject, 'utf-8')  # 邮件标题
        msg['from'] = self.sender  # 发信人地址
        msg['to'] = email  # 收信人地址

        smtp = smtplib.SMTP()
        smtp.connect('smtp.qq.com')
        smtp.login(self.sender, self.password)
        smtp.sendmail(self.sender, email, msg.as_string())  # 这行代码解决的下方554的错误
        smtp.quit()


emails = Email()
repass = Reass()
if __name__ == '__main__':
    emails.run("aaa", "fewfewf", "1161127823@qq.com")
